//
//  SQGoodsListViewCell.m
//  pai
//
//  Created by pro on 14-11-20.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQGoodsListViewCell.h"
#import "UIImageView+WebCache.h"
#import "Global.h"
#import "ASIHTTPRequest.h"

@implementation SQGoodsListViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"SQGoodsListViewCell" owner:self options: nil];
        if(arrayOfViews.count < 1){return nil;}
        if(![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]){
            return nil;
        }
        self = [arrayOfViews objectAtIndex:0];
    }
    return self;
}

-(void)setDic:(NSDictionary*)dic
{
    
}
-(void)setItem:(product_item_t)item
{
    self.nameLabel.text = [NSString stringWithUTF8String:item.title];
    self.priceLabel.text = [NSString stringWithFormat:@"%d豆",item.price];
    self.pointLabel.text = [NSString stringWithFormat:@"%d分",item.points];
    
    //if (self.imageUrl == nil) {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/products/%d/photos.json", IMAGESERVICE,item.product_id]];
        NSLog(@"get image url:%@",url);
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        
        [request startSynchronous];
        
        NSError *error = [request error];
        
        if (!error) {
            
            NSString *response = [request responseString];
            NSLog(@"%@",response);
            
            NSData *data= [response dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error = nil;
            NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            NSString* imageUrl = [NSString stringWithFormat:@"%@%@",IMAGESERVICE,[dic objectForKey:@"home"]];
            self.imageDic = dic;
            self.imageUrl =imageUrl;
        }
    //}
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.imageView setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:nil];
}
@end
